package com.jerei.plugin.control;

import com.jerei.plugin.entity.WcmCommonWebsite;
import com.jerei.plugin.service.IWcmCommonWebsiteService;
import com.jerei.utils.UtilsCommon;
import com.jerei.utils.UtilsCondition;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsUI;
import com.jerei.web.utils.UtilsWeb;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;

/**
 * Created by sunfeihu on 2016/10/21.
 */
@Extension
public class WcmCommonWebsiteControl extends PluginController {

    @Autowired
    private IWcmCommonWebsiteService iWcmCommonWebsiteService;

    @RequiresPermissions({"common-website:save", "common-website:update"})
    public String save() {
        WcmCommonWebsite entity = this.wrapper(WcmCommonWebsite.class);
        if (entity.getId() != null && entity.getId() > 0L) {
            iWcmCommonWebsiteService.update(entity);
        } else {
            iWcmCommonWebsiteService.save(entity);
        }
        return UtilsUI.ok();
    }

    @RequiresPermissions({"common-website:remove"})
    public String delete() {
        String[] ids = this.getRequest().getParameterValues("ids");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                iWcmCommonWebsiteService.delete(UtilsCommon.getLong(ids[i]));
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }


    @RequiresPermissions({"common-website:save", "common-website:update", "common-website:view"})
    public void edit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        WcmCommonWebsite entity = null;
        if (null != id) {
            entity = iWcmCommonWebsiteService.find(id);
        } else {
            Long gid = UtilsWeb.getLong(this.getRequest(), "gid");
            entity = new WcmCommonWebsite();
            entity.setGid(gid);
        }
        this.getModel().addAttribute("obj", entity);
    }


    @RequiresPermissions({"common-website:view"})
    public String page() {
        Long gid = UtilsWeb.getLong(this.getRequest(), "search.eq.gid");
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        if (null != gid && gid > 0L) {
            return UtilsUI.datagrid(iWcmCommonWebsiteService.page(nowPage, pageSize, UtilsCondition.getConditionEntity(this.getRequest().getParameterMap())));
        } else {
            return UtilsUI.datagrid();
        }

    }


}